# -*- coding: utf-8 -*-
class PromotionController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  
  def promotionList

  end

  def newPromotion

  end

  def editPromotion

  end

  def create
    begin
      promotion = Promotion.new()
      promotion.company_id = session[:company_id]
      promotion.department_id = session[:department_id]
      promotion.employee_id = session[:employee_id]
      promotion.name = params[:name]
      promotion.begin_date = params[:begin_date]
      promotion.end_date = params[:begin_date]
      promotion.description = params[:description]
      promotion.save!
      info = "成功"
    rescue Exception => e
      info = e
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def update
    begin
      promotion = Promotion.find(params[:id])
      promotion.employee_id = session[:employee_id]
      promotion.name = params[:name]
      promotion.begin_date = params[:begin_date]
      promotion.end_date = params[:begin_date]
      promotion.description = params[:description]
      promotion.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def getAllPromotions
    promotions = Promotion.all
    jsonStr="{totalProperty:"+promotions.length.to_s+",root:"+promotions.to_json(:include=>[:employee])+"}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def getPromotionById
    promotion = Promotion.find(params[:id])
    rootStr = promotion.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end
end
